<template>
  <div class="forgot-password-page">
    <van-nav-bar title="找回密码" left-arrow fixed placeholder @click-left="router.back()" class="custom-nav" />
    <div class="forgot-password-content">
      <div class="forgot-password-header">
        <img src="@/assets/images/logo.png" alt="logo" class="logo">
        <h2 class="welcome">重置密码</h2>
        <p class="subtitle">请填写以下信息重置密码</p>
      </div>

      <van-form @submit="onSubmit" ref="formRef">
        <div class="forgot-password-form">
          <div class="input-wrapper">
            <van-field
              v-model="formData.phone"
              type="tel"
              name="phone"
              placeholder="请输入手机号"
              class="custom-field"
              :rules="phoneRules"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="phone-o" />
              </template>
            </van-field>
            <van-field
              v-model="formData.sms"
              center
              name="sms"
              placeholder="请输入验证码"
              class="custom-field"
              :rules="codeRules"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="shield-o" />
              </template>
              <template #button>
                <van-button
                  size="small"
                  type="primary"
                  class="code-btn"
                  :disabled="!!countdown || !formData.phone || !/^1[3-9]\d{9}$/.test(formData.phone)"
                  @click="sendSms"
                  style="z-index: 1;"
                >
                  {{ countdown ? `${countdown}s后重新获取` : '获取验证码' }}
                </van-button>
              </template>
            </van-field>
            <van-field
              v-model="formData.newPassword"
              :type="showPassword ? 'text' : 'password'"
              name="newPassword"
              placeholder="请输入新密码"
              class="custom-field"
              :rules="passwordRules"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
              <template #right-icon>
                <van-icon 
                  :name="showPassword ? 'eye-o' : 'closed-eye'" 
                  @click="showPassword = !showPassword"
                  class="eye-icon"
                />
              </template>
            </van-field>
            <van-field
              v-model="formData.confirmPassword"
              :type="showConfirmPassword ? 'text' : 'password'"
              name="confirmPassword"
              placeholder="请确认新密码"
              class="custom-field"
              :rules="confirmPasswordRules"
              :border="false"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
              <template #right-icon>
                <van-icon 
                  :name="showConfirmPassword ? 'eye-o' : 'closed-eye'" 
                  @click="showConfirmPassword = !showConfirmPassword"
                  class="eye-icon"
                />
              </template>
            </van-field>
          </div>

          <van-button 
            block 
            round 
            type="primary" 
            native-type="submit"
            class="submit-btn"
            :loading="loading"
          >
            重置密码
          </van-button>

          <div class="action-links">
            <span class="link" @click="router.push('/auth/login/phone')">手机号登录</span>
            <span class="link" @click="router.push('/auth/login')">返回登录</span>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/user'
import { phoneRules, codeRules, passwordRules, generateConfirmPasswordRules } from '@/utils/validate'
import { useCountdown } from '@/composables/useCountdown'
import type { FormInstance } from 'vant'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref<FormInstance>()
const { countdown, startCountdown, stopCountdown } = useCountdown()

onUnmounted(() => {
  stopCountdown()
})

const formData = reactive({
  phone: '13297556624',
  sms: '',
  newPassword: '',
  confirmPassword: ''
})

const loading = ref(false)

const confirmPasswordRules = computed(() =>
  generateConfirmPasswordRules(formData.newPassword)
)

const showPassword = ref(false)
const showConfirmPassword = ref(false)

const sendSms = async () => {
  try {
    if (!formRef.value) {
      return
    }
    
    await formRef.value?.validate('phone')
    await userStore.sendSms(formData.phone)
    showToast('验证码已发送')
    startCountdown()
  } catch (error: any) {
    console.log(error?.message);
    
    showToast(error?.message || '发送失败')
  }
}

const onSubmit = async () => {
  try {
    loading.value = true
    await userStore.resetPassword({
      phone: formData.phone,
      sms: formData.sms,
      newPassword: formData.newPassword
    })
    showToast('密码重置成功')
    router.push('/auth/login')
  } catch (error: any) {
    showToast(error.message || '重置失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
// 复用登录页面的样式，只需要修改类名
.forgot-password-page {
  min-height: 100vh;
  // background: linear-gradient(to bottom, #F8E6FF, #F0E6FF);
  
  .custom-nav {
    background: transparent;
    :deep(.van-nav-bar__title) {
      color: #333;
      font-size: 18px;
      font-weight: 500;
    }
    :deep(.van-icon) {
      color: #333;
      font-size: 20px;
    }
  }
}

// 其余样式与登录页完全相同，只需要将 login 替换为 forgot-password
.forgot-password-content {
  padding: 30px 20px 30px; // 顶部padding增加，为导航栏留出空间
}

.forgot-password-header {
  text-align: center;
  margin-bottom: 30px;
  .logo {
    width: 72px;
    height: 72px;
    border-radius: 16px;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .welcome {
    font-size: 22px;
    font-weight: 600;
    color: #333;
  }

  .subtitle {
    font-size: 14px;
    color: #666;
  }
}

// 复用登录页面的表单样式
.forgot-password-form {
  margin-bottom: 45px;
  
  .input-wrapper {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 25px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    position: relative;

    :deep(.custom-field) {
      background: transparent;
      padding: 5px 0;
      // height: 48px;
      // margin-bottom: 12px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.08);

      .van-field__error-message {
        position: absolute;
        left: 0px; // 与输入框文本对齐
        bottom: -7px;
        font-size: 12px;
        // color: #ff4646; // 更柔和的红色
        white-space: nowrap; // 防止文字换行
      }

      // 输入框获得焦点时的样式
      &.van-field--error {
        .van-field__control {
          color: #ff4646;
        }
        
        .van-field__left-icon .van-icon {
          color: #ff4646;
        }
      }

      &:last-child {
        margin-bottom: 0;
        border-bottom: none;
      }
      .van-field__control {
        font-size: 16px;
        color: #333;
        height: 48px;
        padding-left: 8px;

        &::placeholder {
          color: #999;
          font-size: 15px;
        }
      }

      .van-field__left-icon {
        display: flex;
        align-items: center;
        margin-right: 4px;

        .van-icon {
          font-size: 20px;
          color: #666;
        }
      }

      .code-btn {
        height: 36px;
        padding: 0 16px;
        border-radius: 18px;
        background: $primary-color !important;
        border: none;
        font-size: 14px;
      }
    }
  }

  .submit-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    background: #8A2BE2;
    border: none;
    margin: 30px 0 24px;
  }

  .action-links {
    display: flex;
    justify-content: center;
    padding: 0 8px;

    .link {
      color: #8A2BE2;
      font-size: 14px;
      cursor: pointer;
      padding: 4px;

      &:active {
        opacity: 0.7;

      }
    }
  }

  
}
</style>




















